#Fig.3
library(metafor)
library(readxl)
library(ggplot2)
library(ggsignif)
library(ggpubr)

rrdata <- as.data.frame(read_excel("Dataset.xlsx", sheet = "Sheet1"))
rrdata$AvaiP <- sqrt(rrdata$AvaiP)

range01 <- function(x){
  (x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
rrdata <- rrdata %>%
  mutate(
    BG_std  = range01(BG),
    NAG_std = range01(NAG),
    Enzyme_mean = rowMeans(cbind(BG_std, NAG_std), na.rm = TRUE)
  )
AM_data  <- filter(rrdata, Mycorrhizal_types == "AM")
ECM_data <- filter(rrdata, Mycorrhizal_types == "ECM")
get_regression_stats <- function(data, response, predictor) {
  formula <- as.formula(paste(response, "~", predictor))
  model <- lm(formula, data = data)
  sm <- summary(model)
  r_squared <- sm$r.squared
  beta      <- coef(sm)[2, 1]     
  p_value   <- coef(sm)[2, 4]
  r_value <- sign(beta) * sqrt(r_squared)
  return(
    data.frame(
      Predictor = predictor,
      r         = r_value,
      R_squared = r_squared,
      P_value   = p_value
    )
  )
}
variables <- c("TP", "AvaiP", "PGPM", "Enzyme_mean",
               "RD", "SRL", "RTD", "RNC")

AM_results <- do.call(
  rbind,lapply(variables, get_regression_stats,
               data = AM_data, response = "Exudation")
)
ECM_results <- do.call(
  rbind,
  lapply(variables, get_regression_stats,
         data = ECM_data, response = "Exudation")
)

cat("AM Tree Species:\n")
print(AM_results)
cat("\nECM Tree Species:\n")
print(ECM_results)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3a"))
rrdata <- rrdata[, c("Exudation","TP", "Mycorrhizal_types")]
p1 <- ggplot(rrdata, aes(x =TP, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) +  
  geom_smooth(method = "lm",formula = y ~ x, aes(x =TP, y = Exudation, color = Mycorrhizal_types),se=FALSE,size=0.4)+
  labs( x = expression("Total P (g kg"^"-1"*")"),   
        y = expression(paste("Root exudation rate (", mu, "g C ", g^{-1}, " ", h^{-1}, ")"))) +  
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.11, ", italic(P), " < 0.001")), 
                              "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.24, ", italic(P), " < 0.001"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.11, ", italic(P), " < 0.001")), 
                               "ECM"=expression(paste("ECM : ", italic(R)^2, " = 0.24, ", italic(P), " < 0.001"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() + 
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p1)


rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3b"))
rrdata$AvaiP <- sqrt(rrdata$AvaiP)
rrdata <- rrdata[, c("Exudation","AvaiP", "Mycorrhizal_types")]
p2 <- ggplot(rrdata, aes(x = AvaiP, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) + 
  geom_smooth(method = "lm",formula = y ~ x, aes(x =AvaiP, y = Exudation, color = Mycorrhizal_types),se=FALSE,size=0.4)+
  labs(x = expression("Available P (mg kg"^"-1"*", sqrt)"), 
       y = " ") + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.08, ", italic(P), " = 0.002")), 
                              "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.24, ", italic(P), " < 0.001"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.08, ", italic(P), " = 0.002")), 
                               "ECM"=expression(paste("ECM : ", italic(R)^2, " = 0.24, ", italic(P), " < 0.001"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text  = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p2)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3c"))
rrdata <- rrdata[, c("Exudation","PGPM", "Mycorrhizal_types")]
p3 <- ggplot(rrdata, aes(x =PGPM, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) + 
  geom_smooth(method = "lm",formula = y ~ x, aes(x =PGPM, y = Exudation, color = Mycorrhizal_types),se=FALSE,size=0.4)+
  labs(x = "Relative abundance of PGPM (%)", 
       y = " ") + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.06, ", italic(P), " = 0.009")), 
                              "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.08, ", italic(P), " = 0.009"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.06, ", italic(P), " = 0.009")), 
                               "ECM"=expression(paste("ECM : ", italic(R)^2, " = 0.08, ", italic(P), " = 0.009"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() + 
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p3)


rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3d"))
range01 <- function(x){
  (x - min(x, na.rm = TRUE)) /
    (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
rrdata <- rrdata %>%
  mutate(
    BG_std  = range01(BG),
    NAG_std = range01(NAG),
    Enzyme_mean = rowMeans(cbind(BG_std, NAG_std), na.rm = TRUE)
  )
rrdata <- rrdata[, c("Exudation","Enzyme_mean", "Mycorrhizal_types")]
p4 <- ggplot(rrdata, aes(x = Enzyme_mean, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) +  
  geom_smooth(method = "lm", formula = y ~ x, aes(x = Enzyme_mean, y = Exudation, color = Mycorrhizal_types), se = FALSE, size = 0.4) +
  labs(x = "Enzyme activities (scaled)",
       y = " ") +  
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM" = expression(paste("AM : ",italic(R)^2, " = 0.07, ", italic(P), " = 0.003")), 
                              "ECM"= expression(paste("ECM : ", italic(R)^2, " = 0.10, ", italic(P), " = 0.003")))) +
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM" = expression(paste("AM : ",italic(R)^2, " = 0.07, ", italic(P), " = 0.003")), 
                               "ECM"= expression(paste("ECM : ", italic(R)^2, " = 0.10, ", italic(P), " = 0.003")))) +
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() +  
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p4)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3e"))
rrdata <- rrdata[, c("Exudation","RD", "Mycorrhizal_types")]
p5 <- ggplot(rrdata, aes(x =RD, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) + 
  geom_smooth(method = "lm",formula = y ~ x, aes(x =RD, y = Exudation, color = Mycorrhizal_types),se=FALSE,size=0.4)+
  labs(x = "Root diameter (mm)",  
       y = expression(paste("Root exudation rate (", mu, "g C ", g^{-1}, " ", h^{-1}, ")"))) + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.07, ", italic(P), " = 0.004")), 
                              "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.05, ", italic(P), " = 0.036"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.07, ", italic(P), " = 0.004")), 
                               "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.05, ", italic(P), " = 0.036"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() +  
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p5)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3f"))
rrdata <- rrdata[, c("Exudation","SRL", "Mycorrhizal_types")]
p6 <- ggplot(rrdata, aes(x =SRL, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3) +  
  geom_smooth(method = "lm",formula = y ~ x, aes(x =SRL, y = Exudation, color = Mycorrhizal_types),se=FALSE,size=0.4)+
  labs(x = expression("Specific root length (m g"^"-1"*")"), y = " ") + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.15, ", italic(P), " < 0.001")), 
                              "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.20, ", italic(P), " < 0.001"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.15, ", italic(P), " < 0.001")), 
                               "ECM"=expression(paste("ECM : ",italic(R)^2, " = 0.20, ", italic(P), " < 0.001"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() + 
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p6)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3g"))
rrdata <- rrdata[, c("Exudation","RTD", "Mycorrhizal_types")]
p7 <- ggplot(rrdata, aes(x =RTD, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3, linetype = 0) +
  geom_smooth(method = "lm", formula = y ~ x, aes(color = Mycorrhizal_types, linetype = Mycorrhizal_types), se = FALSE, size = 1, show.legend = FALSE) +
  labs(x = expression("Root tissue density (g cm"^"-3"*")"), y = " ") + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.04, ", italic(P), " = 0.022")), 
                              "ECM"=expression(paste("ECM : ", italic(P), " = 0.332"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.04, ", italic(P), " = 0.022")), 
                               "ECM"=expression(paste("ECM : ", italic(P), " = 0.332"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() +  
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p7)

rrdata <- as.data.frame(read_excel("Source Data_Fig. 3.xlsx", sheet = "Fig. 3h"))
rrdata <- rrdata[, c("Exudation","RNC", "Mycorrhizal_types")]
p8 <- ggplot(rrdata, aes(x =RNC, y = Exudation, color = Mycorrhizal_types, fill = Mycorrhizal_types)) +
  geom_point(shape = 21, size = 3, stroke = 0.2, color = "black") +
  geom_smooth(method = "lm", se = TRUE, size = 1, alpha = 0.3, linetype = 0) +
  geom_smooth( method = "lm", formula = y ~ x, aes(color = Mycorrhizal_types, linetype = Mycorrhizal_types), se = FALSE, size = 1, show.legend = FALSE) +
  labs(x = expression("Root nitrogen content (mg g"^"-1"*")"), y = " ") + 
  scale_fill_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                    labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.09, ", italic(P), " < 0.001")), 
                              "ECM"=expression(paste("ECM : ", italic(P), " = 0.722"))))+
  scale_color_manual(values = c("AM" = "#F39F4E", "ECM" = "#98CFE6"),
                     labels= c("AM"=expression(paste("AM : ",italic(R)^2, " = 0.09, ", italic(P), " < 0.001")), 
                               "ECM"=expression(paste("ECM : ", italic(P), " = 0.722"))))+
  scale_y_continuous(limits = c(60, 450), breaks = c(100, 200, 300, 400)) +
  theme_bw() + 
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, colour = "black"),
    axis.text = element_text(size = 14, colour = "black"),
    legend.position = c(0.5, 0.88),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.border = element_rect(size = 0.9, color = "black")
  )

print(p8)

Fig.3 <- ggarrange(p1, p2, p3, p4, p5, p6, p7, p8,
                   ncol =4, nrow = 2, #common.legend = TRUE,legend = "bottom",
                   labels = c("a","b","c","d","e","f","g","h"), font.label=list(size= 22), hjust = -1, vjust = 1) 
Fig.3
ggsave("Fig.3.jpg", Fig.3, units="cm", width=40, height=20, dpi=1000)
